const aedes = require('aedes')()
const aedesServer = require('net').createServer(aedes.handle);
const  {log,logI}  = require('./log.js')
const dotenv =  require('dotenv');
const {join}=require('path');
dotenv.config({path:join(__dirname,'../','.env')});
const port = (process.env.MQTT_PORT||1883);
/*const settings = {
    interfaces: [
        { type: "mqtt", port: 1883, bundle: true },
        { type: "http", port: 3005, bundle: true },
    ]
};*/
aedesServer.listen(port, function () {
    log.info('服务启动并开始监听端口：', port)
})

aedes.on("client", (client) => {
    log.info("客户端连接成功:", client.id);
})

//监听客户端发布的主题
aedes.on("publish", function (packet, client) {
    //针对不同的客户端写不同的逻辑
    //根据主题不同写不同的逻辑，建议采用JSON格式传输
    //logI("C-S", packet.topic, "MQTT", `${packet.payload.toString()}`, "null")
    let topic = packet.topic;
    switch (topic) {
        case 'web-control':
            logI("C-S", packet.topic, "web-control", `${packet.payload.toString()}`, "null")
            break;
        default:
            break;
    }
})